<?php
require '../data/data.php';

session_start();

$isLogout = false;
if (isset($_SESSION['logout'])&&$_SESSION['logout']===true) {
	$isLogout = true;
	setcookie('remember-token','',time()-3600);
	unset($_SESSION['logout']);
}

$isSignin = isset($_SESSION['user'])? true : false;
$user = isset($_SESSION['user'])? $_SESSION['user'] : null;

if (isset($_COOKIE['remember-token']) && !empty($_COOKIE['remember-token']) && !$isLogout) {
	if (!$isSignin) {
		$userId = $_COOKIE['user-id'];
		$rememberToken = $_COOKIE['remember-token'];
		$sql = 'select id,user_name,password,remember_token,token_salt from users where id=?';
		$data = query($sql,[$userId]);
		$user = $data[0];
		$salt = $user['token_salt'];
		$seToken = hash('sha256', $rememberToken . $salt);

		if ($seToken === $user['remember_token']) {
			$_SESSION['user'] = $user;
			$isSignin = true;
		}
	}
}

if (isset($_SESSION['remember-token'])&&!empty($_SESSION['remember-token'])) {
	$rememberToken = $_SESSION['remember-token'];
	$userId = $user['id'];
	unset($_SESSION['remember-token']);
	setcookie('remember-token',$rememberToken,time()+3600*24);
	setcookie('user-id',$userId,time()+3600*24);
}

$goods = query('select * from goods');

$countOfCart = 0;
if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
    $cart = $_SESSION['cart'];
    $countOfCart = count($cart);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>凡客</title>
<link rel="stylesheet" type="text/css" href="css/vancl.css"/>
</head>
<body>
			
		
		<!--顶部开始-->
		<div id="top">
			<div class="top_container">
				<div class="top_right">
					
				<!--已登录-->	
				<!--<span class="sp1">
					您好,<span class="username">15641523</span>!
				</span>
				<a href="" class="login">退出登录</a>
				
				<span class="sp2">|</span>
				
				<a href="" class="reg">更换用户</a>-->
				<!--已登录-->	
				
				
				<!--未登录-->
				<span class="sp1">
					您好,欢迎光临凡客诚品!
				</span>
				<?php if ($isSignin):?>
				<a href="#" class="login"><?=$user['user_name'] ?></a>
				
				<span class="sp2">|</span>
				
				<a href="../action/logout_action.php" class="reg">退出</a>
			    <?php else: ?>
				<a href="signin.php" class="login">登录</a>
				
				<span class="sp2">|</span>
				
				<a href="register.php" class="reg">注册</a>
				<?php endif; ?>
				<!--未登录-->
				<a href="" class="order">我的订单</a>
				
				<img src="img/notice.png"/ class="notice">
				
				<a href="" class="webgg">网站公告</a>
				
				<img src="img/w1.jpg"/ class="w1">
				
				<img src="img/w2.jpg"/ class="w2">
				
				</div>

			</div>			
		</div>
		<!--顶部结束-->
		
		
		<!--头部搜索开始-->
		<div id="search">
			<div class="search_area">
							
				<div class="area_top">
					<input type="text" class="search_text" placeholder="搜“羊毛大衣”，体验与众不同"/>
					<div class="search_button"></div>
					<div class="search_shoppingcar"><a href="cart.php"><span class="search_shoppingcar_word">购物车(<?php echo $countOfCart ?>)</span></a></div>
				</div>
				
				<div class="area_bottom">
					<span class="hot_search">热门搜索：</span>
					<a href="" class="hot_search1">T恤</a>
					<a href="" >免烫衬衫</a>
					<a href="" >黑标</a>
					<a href="" >羊毛大衣</a>
					<a href="" >休闲裤</a>
					<a href="" >户外鞋</a>
					<a href="" >袜品</a>
				</div>
			</div>
		</div>
		<!--头部搜索结束-->
		
		
		<!--头部导航栏开始-->
		<div id="head_nav">			
			<img src="img/vancl_logo.png" class="logo"/>
			<a href="" class="nav1">首页</a>
			<a>|</a>
			<a href="" class="nav2">T恤</a>
			<a>|</a>
			<a href="" class="nav3">衬衫</a>
			<a>|</a>
			<a href="" class="nav4">黑标</a>
			<a>|</a>
			<a href="" class="nav5">外套</a>
			<a>|</a>
			<a href="" class="nav6" >水柔棉</a>
			<a>|</a>
			<a href="" class="nav7">裤装</a>
			<a>|</a>
			<a href="" class="nav8">鞋</a>
			<a>|</a>
			<a href="" class="nav9">家居配饰</a>
			<a>|</a>
			<a href="" class="nav10">韩国购</a>
			
		</div>	
		<!--头部导航栏结束-->
	
		<!--商品区域开始-->
		<div id="spring_new">
			<p>春季新品预售</p>
			<?php foreach ($goods as $good): ?>
				<div class="good">
					<img src="<?=$good['image'] ?>" alt="" />
					<p class="gname"><?=$good['name'] ?></p>
					<p class="price">￥<?=$good['price'] ?></p>
					<a href="../action/cart_add_action.php?id=<?=$good['id'] ?>">加入购物车</a>
				</div>
			<?php endforeach; ?>
		</div>
		<!--商品区域结束-->
		
		
		
		
		<!--底部售后-->
		<div id="final">		
			<div class="area1">
				<div class="sh1"><img src="img/sh1.png"/></div>
				<div class="sh2"><img src="img/sh2.png"/></div>
				<div class="sh3"><img src="img/sh3.png"/ class><p>扫描下载<span class="span1">凡客</span>客户端<p> 
				</div>
			</div>
			<div class="area2">
				<div class="d1"><a href="">关于凡客</a></div>
				<div class="d2"><a href="">新手指南</a></div>
				<div class="d3"><a href="">配送范围及时间</a></div>
				<div class="d4"><a href="">支付方式</a></div>
				<div class="d5"><a href="">售后服务</a></div>
				<div class="d6"><a href="">帮助中心</a></div>
			</div>
		</div>
		<!--底部售后结束-->
		
		
		
		<!--尾部-->
		<div id="tail">
		<p>Copyright 2007 - 2016 vancl.com All Rights Reserved 京ICP证100557号 京公网安备11011502002400号 出版物经营许可证新出发京批字第直110138号</p>
		<p>凡客诚品（北京）科技有限公司</p>
		<ul>
			<li><div class="t1"></div></li>
			<li><div class="t2"></div></li>
			<li><div class="t3"></div></li>
			<li><div class="t4"></div></li>
			<li><div class="t5"></div></li>
			<li><div class="t6"></div></li>
		</ul>
		</div>
		<!--尾部结束-->
	</body>
</html>